package cn.sunlight.mm.controller;

import cn.sunlight.framework.annotation.HmComponent;
import cn.sunlight.framework.annotation.HmRequestMapping;
import cn.sunlight.framework.annotation.HmSetter;
import cn.sunlight.mm.base.BaseController;
import cn.sunlight.mm.common.GlobalConst;
import cn.sunlight.mm.entity.PageResult;
import cn.sunlight.mm.entity.QueryPageBean;
import cn.sunlight.mm.entity.Result;
import cn.sunlight.mm.pojo.Question;
import cn.sunlight.mm.pojo.User;
import cn.sunlight.mm.service.QuestionService;
import lombok.extern.slf4j.Slf4j;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author sunlight
 * @description: 基础题库控制器
 * @date 2021/5/20
 * @Version v1.0
 */
@Slf4j
@HmComponent
public class QuestionController extends BaseController {

    @HmSetter("questionService")
    QuestionService questionService;

    @HmRequestMapping("/question/findClassicListByPage")
    public void findClassicList(HttpServletRequest request, HttpServletResponse response) throws Exception {
        QueryPageBean pageBean = parseJSON2Object(request, QueryPageBean.class);
        if (pageBean==null){
            pageBean = new QueryPageBean();
            pageBean.setCurrentPage(1);
            pageBean.setPageSize(15);
        }
        log.info("questionList pageBean:{}",pageBean);

        PageResult listByPage=questionService.findClassicByPage(pageBean);

        printResult(response,new Result(true,"获取经典题目列表成功",listByPage));
    }

    @HmRequestMapping("/question/addOrUpdate")
    public void addOrUpdate(HttpServletRequest request, HttpServletResponse response) throws Exception {
        try {
            Question question = parseJSON2Object(request, Question.class);
            log.debug("调用service：question:{}",question);

            User user = getSessionUser(request, GlobalConst.SESSION_KEY_USER);

            question.setUserId(user!=null?user.getId():1);

            questionService.addOrUpdate(question);

            printResult(response,new Result(false,"更新成功"));
        }catch (RuntimeException e){
            e.printStackTrace();
            log.error("",e);
            printResult(response,new Result(false,"更新失败"+e.getMessage()));
        }
    }

    /**
     * 分页获取基础题目列表
     * @param request
     * @param response
     * @throws Exception
     */
    @HmRequestMapping("/question/findListByPage")
    public void findListByPage (HttpServletRequest request, HttpServletResponse response) throws Exception {
        try {
            QueryPageBean queryPageBean = parseJSON2Object(request, QueryPageBean.class);
            log.debug("queryPageBean...:{}",queryPageBean);
            if (queryPageBean==null){
                queryPageBean = new QueryPageBean();
                queryPageBean.setCurrentPage(1);
                queryPageBean.setPageSize(10);
            }
            PageResult listByPage=questionService.findListByPage(queryPageBean);

            printResult(response,new Result(true,"查询成功",listByPage));
        }catch (RuntimeException e){
            e.printStackTrace();
            log.error("",e);
            printResult(response,new Result(false,""+e.getMessage()));
        }
    }

}
